// miniprogram/pages/comment/comment.js
let page
let musicId
let saveComment
let total
Page({

  /**
   * 页面的初始数据
   */
  data: {
    music: [],
    comments: [],
    flag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    musicId = options.musicId
    page = 0
    this.getMusicDetail(musicId)
    this.getComment(musicId,page)
  },
  getMusicDetail(musicId){
    wx.cloud.callFunction({
      name: 'music',
      data: {
        $url: 'musicdetail',
        musicId
      }
    }).then((res) => {
      this.setData({
        music: res.result.songs[0]
      })
    })
  },
  getComment(musicId,page) {
    wx.showToast({
      title: '正在加载中',
      icon: 'loading',
      duration: 10000
    })
    wx.cloud.callFunction({
      name: 'comment',
      data:{ 
        musicId,
        page
      }
    }).then((res) =>{
      total = res.result.total
      let hotComments = res.result.hotComments || []
      let comments = res.result.comments || []
      if(!comments){
        this.setData({
          flag: false
        })
      }
      if(hotComments.length){
        hotComments.concat(comments)
        this.setData({
          comments: hotComments
        })
      } else {
        if(comments.length){
          this.setData({
            comments: this.data.comments.concat(comments)
          })
        } 
      } 
      wx.hideToast()
    })
  },
  onClick() {
    wx.navigateBack({
     delta:'1'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const id = musicId
    page++
    let can = total < 5000 ? total : 5000
    if(page * 15 < can) {
      this.getComment(id,page)
    } else {
      wx.hideToast()
      wx.showToast({
        title: '没有更多啦~',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})